$(document).ready((function(){
  $("#submitAnggota").click(function()
  {
      $.post('/aventurine_project/async/getpegawai', 
              {'nik':$("#nik").val()}, 
              function(data){
                  if(data.nik)
                    $("#data").append("<tr id='anggota_"+data.id_pegawai+"'><td>"+data.nik+"</td><td>"+data.nama_pegawai+"</td><td>"+data.email_pegawai+"</td><td><a href=\"javascript:void\" onclick='deleteAnggotaOnClick("+data.id_pegawai+")'><img src='/aventurine_project/content/images/delete.png' /></a></td></tr>");
                  else
                    $("notfound").appendTo($("error"));
              }, 
              'json');
       return false;
  });
  
  
  $("#submitKonsumen").click(function(){
      if($("#id_konsumen").val() != 0)
          var id_konsumen = $("#id_konsumen").val();
      
      $.post('/aventurine_project/async/getkonsumen', 
            {'id_konsumen':id_konsumen}, 
            function(data){
                if(data.id_konsumen) {
                    $("#dataLokasi").empty();
                    $("#dataLokasi").append('<dt>Nama Konsumen</dt><dd>'+data.nama_konsumen+'</dd>');
                    $("#dataLokasi").append('<dt>Nama Perusahaan</dt><dd>'+data.nama_perusahaan+'</dd>');
                    $("#dataLokasi").append('<dt>Alamat</dt><dd>'+data.alamat_konsumen+'</dd>');
                    $("#dataLokasi").append('<dt>Kota</dt><dd>'+data.kota+'</dd>');
                    $("#dataLokasi").append('<dt>Provinsi</dt><dd>'+data.provinsi+'</dd>');
                    $("#dataLokasi").append('<dt>Negara</dt><dd>'+data.negara+'</dd>');
                    $("#dataLokasi").append('<dt>Kode Pos</dt><dd>'+data.kode_pos+'</dd>');
                    $("#dataLokasi").append('<dt>No Telepon</dt><dd>'+data.no_telepon+'</dd>');
                    $("#dataLokasi").append('<dt>Fax</dt><dd>'+data.fax+'</dd>');
                    $("#dataLokasi").append('<dt>Email</dt><dd>'+data.email+'</dd>');
                }
            }, 'json')
      
      return false;
  });

  $("#submitGenset").click(function(){
      if($("#id_genset").val() != 0)
          var id_genset = $("#id_genset").val();
      
      $.post('/aventurine_project/async/getgenset', 
            {'id_genset':id_genset}, 
            function(data){
                if(data.id_genset) {
                    $("#dataGenset").append("<tr id='genset_"+data.id_genset+"'><td>"+data.serial_number_genset+"</td><td>"+data.genset_model+"</td><td>"+data.daya_kva+"</td><td><a href=\"javascript:void\" onclick='deleteGensetOnClick("+data.id_genset+")'><img src='/aventurine_project/content/images/delete.png' /></a></td></tr>");
                } else {
                   $("#project-frag-4").append("<div title='' id='errorGenset' style='font-weight: bold'><center>"+data.response+"</center></div>");
                   $("#errorGenset").dialog({modal: true});
                   setTimeout(function() {$("#errorGenset").remove().fadeOut();}, 1000)
                }
            }, 
            'json');
      return false;
  });

  $("#nik").keyup(function(){
      var nik = $("#nik").val();
      $.ajax({
          type:"POST",
          url:"/aventurine_project/async/getname",
          data:"nik="+nik,
          success: function(data) {
              $("#nama").empty();
              $("#nama_pegawai").val(data);
          }
      });
    });
    
    $("#errorContinue").dialog({modal:true, buttons: {
            Ok: function() {$( this ).dialog( "close" );}}});
    setTimeout(function() {$("#errorContinue").remove().fadeOut();}, 5000)
    
    if($("#nik").val() != null){setInterval(function() {getName();},1000)}
    if($("#daya_kva").val() != null){setInterval(function() {getDaya();},1000)}
}));

function getDaya()
{
    var daya = $("#daya_kva").val();
    $.ajax({
        type:"POST",
        url:site+"/async/getdaya",
        data:"daya="+daya,
        success: function(data) {
            $("#harian").val(data.harian);
            $("#mingguan").val(data.mingguan);
            $("#bulanan").val(data.bulanan);
            $("#id_harga_sewa").val(data.id_harga_sewa);
            $("#harian_bbm").val(data.harian_bbm);
            $("#mingguan_bbm").val(data.mingguan_bbm);
            $("#bulanan_bbm").val(data.bulanan_bbm);
        },
        dataType: "json"
    });
}
    
function getName()
{
    var nik = $("#nik").val();
    $.ajax({
        type:"POST",
        url:"/aventurine_project/async/getname",
        data:"nik="+nik,
        success: function(data) {
            $("#nama").empty();
            $("#nama_pegawai").val(data);
        }
    });  
}